package com.cloud.core.redis;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 1Cloud1
 * @author Administrator
 * @since 2015-11-4
 * @version 1.0
 */
@Component("RedisPoolFactory")
public class RedisPoolFactory {
	@Value("${redis.pool.maxActive}")
	public String maxActive;
	@Value("${redis.pool.maxIdle}")
	public String maxIdle;
	@Value("${redis.pool.maxWait}")
	public String maxWait;
	@Value("${redis.pool.testOnBorrow}")
	public String testOnBorrow;
	@Value("${redis.pool.testOnReturn}")
	public String testOnReturn;
	@Value("${redis.server.host}")
	public String server;
	@Value("${redis.server.port}")
	public String port;
	@Value("${redis.server.auth}")
	public String auth;
	public JedisPool pool;

	@PostConstruct
	public void init(){
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxActive(Integer.valueOf(maxActive).intValue());
		config.setMaxIdle(Integer.valueOf(maxIdle).intValue());
		config.setMaxWait(Long.valueOf(maxWait).longValue());
		config.setTestOnBorrow(Boolean.valueOf(testOnBorrow).booleanValue());
		config.setTestOnReturn(Boolean.valueOf(testOnReturn).booleanValue());
		pool = new JedisPool(config, server, Integer.valueOf(port), 10000, auth);
	}
	
	@PreDestroy
	public void destroy(){
		pool = null;
	}
	
	/**
	 * 获取Redis连接池
	 * @return
	 */
	public JedisPool getInstance() {
		return pool;
	}
}
